package com.dinner.service;

import java.util.List;

import com.dinner.model.MenuItemDao;
import com.dinner.model.Order;
import com.dinner.model.OrderDao;
import com.dinner.util.Command;
import com.dinner.util.CommandInvoker;
import com.dinner.util.CompleteOrderCommand;
import com.dinner.util.HandlerOrderCommand;
import com.dinner.util.OrderSubmissionCommand;
import com.dinner.util.PageHelper;

public class OrderServiceImpl implements OrderService {

	private OrderDao orderDao = null;
	private MenuItemDao menuItemDao = null;
	private CommandInvoker invoker = null;
	private String recounterName = null;

	public String getRecounterName() {
		return recounterName;
	}

	public void setRecounterName(String recounterName) {
		this.recounterName = recounterName;
	}

	/**
	 * @return the orderDao
	 */
	public OrderDao getOrderDao() {
		return orderDao;
	}

	/**
	 * @param orderDao
	 *            the orderDao to set
	 */
	public void setOrderDao(OrderDao orderDao) {
		this.orderDao = orderDao;
	}

	/**
	 * @return the menuItemDao
	 */
	public MenuItemDao getMenuItemDao() {
		return menuItemDao;
	}

	/**
	 * @param menuItemDao
	 *            the menuItemDao to set
	 */
	public void setMenuItemDao(MenuItemDao menuItemDao) {
		this.menuItemDao = menuItemDao;
	}

	@Override
	public void createOrder(String consumerID, int menuID, String address,
			String phone, int serveNumber) {
		/*
		 * DinnerFactory factory = DinnerFactory.getFactory(); Order order =
		 * factory.newOrder(); // Order order = new Order();
		 * order.setConsumerID(consumerID);
		 * order.setMenuItemName(menuItemDao.getMenuItemByID(menuID)
		 * .getMenuItemName());
		 * order.setSellerID(menuItemDao.getSellerByMenuID(menuID));
		 * order.setOrderAddress(address); order.setOrderPhone(phone);
		 * order.setServeCount(serveNumber);
		 * order.setOrderTime(TimeStampUtil.getCurrentTimeInStringFormat());
		 * order.setOrderState(Order.CONSUMER_ORDERED);
		 */

		/*
		 * MenuItem item = menuItemDao.getMenuItemByID(menuID); float price =
		 * item.getMenuItemPrice(); order.setTotalPrice(price serveNumber);
		 * 
		 * orderDao.saveOrder(order);
		 */
		Command orderCommand = new OrderSubmissionCommand(consumerID, menuID,
				address, phone, serveNumber, recounterName, menuItemDao,
				orderDao);
		invoker.setCommand(orderCommand);
		invoker.invokeCommand();
	}

	@Override
	public List<Order> getOrderBySeller(String sellerID, byte state) {
		return orderDao.getOrderBySeller(sellerID, state);
	}

	@Override
	public List<Order> getOrderByConsumer(String consumerID, byte state) {
		return orderDao.getOrderByConsumer(consumerID, state);
	}

	@Override
	public void completeOrder(String consumerID, int orderID) {
		Command completeCommand = new CompleteOrderCommand(consumerID, orderID,
				orderDao);
		invoker.setCommand(completeCommand);
		invoker.invokeCommand();
	}

	@Override
	public void handleOrder(String sellerID, int orderID) {
		/*
		 * orderDao.handleOrder(sellerID, orderID, Order.CONSUMER_ORDERED,
		 * Order.ORDER_HANDLED);
		 */
		Command handlerOrderCommand = new HandlerOrderCommand(sellerID,
				orderID, orderDao);
		invoker.setCommand(handlerOrderCommand);
		invoker.invokeCommand();
	}

	public CommandInvoker getInvoker() {
		return invoker;
	}

	public void setInvoker(CommandInvoker invoker) {
		this.invoker = invoker;
	}

	@Override
	public List<Order> getOrderByConsumerAndPage(String userID, byte state,
			PageHelper p) {
		return orderDao.getOrderByConsumerAndPage(userID, state, p);
	}

	@Override
	public List<Order> getOrderBySellerAndPage(String userID, byte state,
			PageHelper p) {
		return orderDao.getOrderBySellerAndPage(userID, state, p);
	}

}
